package com.sparrow.source.service.impl;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.google.common.collect.Maps;
import com.sparrow.source.persistense.IasDeviceUser;
import com.sparrow.source.persistense.mapper.IasDeviceUserMapper;
import com.sparrow.source.service.IasDeviceUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.MapperMethod;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author bryceyung
 * @since 2024/12/8
 */
@Service
@Slf4j
public class IasDeviceUserServiceImpl extends ServiceImpl<IasDeviceUserMapper, IasDeviceUser> implements IasDeviceUserService {

    @Override
    public Boolean batchCrdData(Integer deviceId, List<IasDeviceUser> deviceUserList) {
        //        List<IasDeviceUser> dbData = this.lambdaQuery().eq(IasDeviceUser::getDeviceId, deviceId).list();
        // 筛选出在deviceUserList中不存在的数据
        //        List<IasDeviceUser> deleteList = dbData.stream().filter(db -> !deviceUserList.stream().anyMatch(param ->
        //                param.getUid().equals(db.getUid()))).collect(Collectors.toList());
        //        if (deleteList.size() > 0) {
        //            deleteList.forEach(data -> {
        //                data.setDeleted(true);
        //            });
        //            this.saveOrUpdateBatch(deleteList);
        //        }
        log.info("[batchCrdData] saveOrUpdateBatch end");
        Map<String, Object> map = Maps.newConcurrentMap();
        map.putIfAbsent("device_id", deviceId);
        baseMapper.deleteByMap(map);
        return saveOrUpdateBatch(deviceUserList);
    }

    @Override
    public Boolean saveOrUpdateBatchByUid(Integer deviceId, List<IasDeviceUser> userList) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
        Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
        String keyProperty = "uid";
        Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
        return executeBatch(userList, 1000, (sqlSession, entity) -> {
            Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
            if (StringUtils.checkValNull(idVal) || Objects.isNull(getByUid(deviceId, Integer.valueOf(idVal.toString())))) {
                sqlSession.insert(tableInfo.getSqlStatement(SqlMethod.INSERT_ONE.getMethod()), entity);
            } else {
                MapperMethod.ParamMap<IasDeviceUser> param = new MapperMethod.ParamMap<>();
                param.put(Constants.ENTITY, entity);
                sqlSession.update(tableInfo.getSqlStatement(SqlMethod.UPDATE_BY_ID.getMethod()), param);
            }
        });
    }

    @Override
    public IasDeviceUser getByUid(Integer deviceId, Integer uid) {
        return lambdaQuery().eq(IasDeviceUser::getDeviceId, deviceId).eq(IasDeviceUser::getUid, uid).one();
    }

    @Override
    public List<IasDeviceUser> queryDeviceUserList(Integer deviceId) {
        return baseMapper.selectDeviceUserList(deviceId);
    }

    @Override
    public IasDeviceUser queryByUid(Integer uid,Integer deviceId) {
        return baseMapper.selectByUid(uid,deviceId);
    }

}
